package com.example.huixue.mapper;

import com.example.huixue.entity.Favorite;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface FavoriteMapper {
    
    /**
     * 添加收藏
     */
    @Insert("INSERT INTO favorites (user_id, resource_id, created_at) " +
            "VALUES (#{userId}, #{resourceId}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Favorite favorite);
    
    /**
     * 取消收藏
     */
    @Delete("DELETE FROM favorites WHERE user_id = #{userId} AND resource_id = #{resourceId}")
    int deleteByUserAndResource(@Param("userId") Long userId, @Param("resourceId") Long resourceId);
    
    /**
     * 检查用户是否已收藏该资源
     */
    @Select("SELECT COUNT(*) > 0 FROM favorites WHERE user_id = #{userId} AND resource_id = #{resourceId}")
    boolean existsByUserAndResource(@Param("userId") Long userId, @Param("resourceId") Long resourceId);
    
    /**
     * 根据用户ID获取收藏列表（分页）
     */
    @Select("SELECT f.id, f.user_id, f.resource_id, f.created_at, " +
            "r.title as resource_title, r.description, r.file_path, r.file_type, r.file_size, " +
            "r.download_count, r.view_count, r.rating, r.created_at as upload_time, " +
            "u.username as uploader_name, c.category_name " +
            "FROM favorites f " +
            "LEFT JOIN resources r ON f.resource_id = r.resource_id " +
            "LEFT JOIN users u ON r.uploader_id = u.user_id " +
            "LEFT JOIN categories c ON r.category_id = c.category_id " +
            "WHERE f.user_id = #{userId} " +
            "ORDER BY f.created_at DESC LIMIT #{offset}, #{limit}")
    List<Favorite> findByUserWithPagination(@Param("userId") Long userId, 
                                             @Param("offset") int offset, @Param("limit") int limit);
    
    /**
     * 获取用户收藏数量
     */
    @Select("SELECT COUNT(*) FROM favorites WHERE user_id = #{userId}")
    long countByUser(Long userId);
    
    /**
     * 获取资源被收藏数量
     */
    @Select("SELECT COUNT(*) FROM favorites WHERE resource_id = #{resourceId}")
    long countByResource(Long resourceId);
    
    /**
     * 根据ID查找收藏记录
     */
    @Select("SELECT f.id, f.user_id, f.resource_id, f.created_at, " +
            "r.title as resource_title, r.description, r.file_path, r.file_type, r.file_size, " +
            "r.download_count, r.view_count, r.rating, r.created_at as upload_time, " +
            "u.username as uploader_name, c.category_name " +
            "FROM favorites f " +
            "LEFT JOIN resources r ON f.resource_id = r.resource_id " +
            "LEFT JOIN users u ON r.uploader_id = u.user_id " +
            "LEFT JOIN categories c ON r.category_id = c.category_id " +
            "WHERE f.id = #{id}")
    Favorite findById(Long id);
    
    /**
     * 获取最近收藏的资源
     */
    @Select("SELECT f.id, f.user_id, f.resource_id, f.created_at, " +
            "r.title as resource_title, r.description, r.file_path, r.file_type, r.file_size, " +
            "r.download_count, r.view_count, r.rating, r.created_at as upload_time, " +
            "u.username as uploader_name, c.category_name " +
            "FROM favorites f " +
            "LEFT JOIN resources r ON f.resource_id = r.resource_id " +
            "LEFT JOIN users u ON r.uploader_id = u.user_id " +
            "LEFT JOIN categories c ON r.category_id = c.category_id " +
            "WHERE f.user_id = #{userId} " +
            "ORDER BY f.created_at DESC LIMIT #{limit}")
    List<Favorite> findRecentByUser(@Param("userId") Long userId, @Param("limit") int limit);
    
    /**
     * 获取热门收藏资源
     */
    @Select("SELECT r.resource_id, r.title, COUNT(f.id) as favorite_count " +
            "FROM favorites f " +
            "JOIN resources r ON f.resource_id = r.resource_id " +
            "WHERE r.status = 'APPROVED' " +
            "GROUP BY r.resource_id, r.title " +
            "ORDER BY favorite_count DESC LIMIT #{limit}")
    List<Map<String, Object>> findPopularFavorites(@Param("limit") int limit);
    
    /**
     * 删除资源的所有收藏记录
     */
    @Delete("DELETE FROM favorites WHERE resource_id = #{resourceId}")
    int deleteByResource(Long resourceId);
} 